﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ThunderSmurfsTDS
{
    public partial class Frm_Judge : Form
    {
        List<TargetingInfo.TargetInfo> listTargets;
        MissileControl.MissileControl mc;
        ImageDetection.CameraDetect cd;
        public static TargetingInfo.TurretAndCameraProperties props;
        WinFormCharpWebCam.WebCam webcam;
        int minutes;
        int seconds;
        int ticks;
        public Frm_Judge()
        {
            InitializeComponent();
            listTargets = new List<TargetingInfo.TargetInfo>();
            cd = new ImageDetection.CameraDetect();
            cd.OnUpdate += updateBitmap;
            cd.GetCameraImage += getImage;
            mc = new MissileControl.MissileControl();
            props = new TargetingInfo.TurretAndCameraProperties();
            //cd.toggleImageDetect();
            this.FormClosing += new FormClosingEventHandler(Frm_Judge_FormClosing);
            mc.startThread();
            webcam = new WinFormCharpWebCam.WebCam();
            webcam.InitializeWebCam(ref liveVideo);
            webcam.Start();
            timer1.Interval = 100;
            minutes = 0; seconds = 0; ticks = 0;
            
        }

        void Frm_Judge_FormClosing(object sender, FormClosingEventArgs e)
        {
            mc.stopThread();
        }

        private void btn_Input_Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = System.IO.Directory.GetCurrentDirectory();
            DialogResult dr = openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                mc.ResetCommand();
                TargetingInfo.TargetsIni tIni = TargetingInfo.TargetsIni.createNewIniReader(openFileDialog1.FileName);
                int group = 0;
                foreach (char c in "abcd")
                {
                    TargetingInfo.TargetInfo ti = new TargetingInfo.TargetInfo();
                    try
                    {
                        group = tIni.groupExists(group,"Target");
                        ti.IsFoe = !bool.Parse(tIni.getKeyValue(group, "Friend"));
                        ti.Z = double.Parse(tIni.getKeyValue(group, "Distance"));
                        ti.X = double.Parse(tIni.getKeyValue(group, "PositionX"));
                        ti.Y = double.Parse(tIni.getKeyValue(group, "PositionY"));

                    }
                    catch
                    {
                    }
                    ti.Y -= props.TurretHeight;
                    listTargets.Add(ti);
                }
            }
        }

        private void updateBitmap(Bitmap bm)
        {
            pictureBox1.Image = bm;
        }

        private Image getImage()
        {
            return liveVideo.Image;
        }

        private void btn_start_Click(object sender, EventArgs e)
        {
            timer1.Start();
            foreach (TargetingInfo.TargetInfo ti in listTargets)
            {
                mc.AimAndFire(ti);
            }
            mc.FireRemaining();
        }

        private void btn_abort_Click(object sender, EventArgs e)
        {
            mc.stopThread();
            timer1.Stop();
        }

        private void Frm_Judge_Load(object sender, EventArgs e)
        {

        }

        private void Frm_Judge_Shown(object sender, EventArgs e)
        {
            webcam.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            ticks++;
            if(ticks == 10){ticks=0; seconds++;}
            if(seconds==60){seconds=0;minutes++;}
            label1.Text = "Time: " + minutes.ToString() + ":" + seconds.ToString();
        }
    }
}
